define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'campus_cost/index' + location.search,
                    add_url: 'campus_cost/add',
                    edit_url: 'campus_cost/edit',
                    del_url: 'campus_cost/del',
                    multi_url: 'campus_cost/multi',
                    import_url: 'campus_cost/import',
                    table: 'campus_cost',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'course_txt', title: __('Course_id'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"0":__('隐藏'),"1":__('展示')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#choose_course_id').click(function(){
                var url = '/manystore/campus_course_management/choose_index';
                if(!url) return false;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ['700px','500px'],
                    callback:function(value){
                        var arr = $.parseJSON(value);
                        console.log(arr)
                        if(arr.length > 0){
                            var couser_name = '';
                            var couser_id = '';
                            for(var i = 0;i<arr.length;i++){
                                couser_name = couser_name+','+arr[i]['name']

                                couser_id = couser_id+','+arr[i]['id']
                                console.log(arr[i])
                            }
                            couser_name = couser_name.substr(1)
                            couser_id = couser_id.substr(1)
                            $("#couser_name").html(couser_name);
                            $("#c-course_id").val(couser_id);
                            console.log(couser_name)
                            console.log(couser_id)
                        }
                    }
                };
                Fast.api.open(url,'选择项目',options);
            })
            Controller.api.bindevent();
        },
        edit: function () {
            $('#choose_course_id').click(function(){
                var url = '/manystore/campus_course_management/choose_index';
                if(!url) return false;
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: ['700px','500px'],
                    callback:function(value){
                        var arr = $.parseJSON(value);
                        console.log(arr)
                        if(arr.length > 0){
                            var couser_name = '';
                            var couser_id = '';
                            for(var i = 0;i<arr.length;i++){
                                couser_name = couser_name+','+arr[i]['name']
                                couser_id = couser_id+','+arr[i]['id']
                                console.log(arr[i])
                            }
                            couser_name = couser_name.substr(1)
                            couser_id = couser_id.substr(1)
                            $("#couser_name").html(couser_name);
                            $("#c-course_id").val(couser_id);
                            console.log(couser_name)
                            console.log(couser_id)
                        }
                    }
                };
                Fast.api.open(url,'选择项目',options);
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});